package controller;

import enums.SysInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.ClazzM;
import service.ClazzService;
import utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Clazz")
public class ClazzController {

    @Autowired
    ClazzService clazzService;

    @RequestMapping("/gotoget")
    public String gotogettt(){
        return "/WEB-INF/view/classM/classM.jsp";
    }

    @RequestMapping("/get")
    @ResponseBody
    public List<ClazzM> get(HttpServletRequest request, HttpServletResponse response){
        List<ClazzM> clazzes = clazzService.getAll();
        request.setAttribute("result",clazzes);
        return clazzes;
    }


    @RequestMapping("/getOne")
    @ResponseBody
    public List<ClazzM> getOne(@RequestParam(value = "clazzId", required = false) Integer clazzId, HttpServletRequest request, HttpServletResponse response){
        if (clazzId == null){
            return clazzService.getAll();
        }
        List<ClazzM> clazzMList = new ArrayList<>();
        ClazzM clazzM = clazzService.getOne(clazzId);
        clazzMList.add(clazzM);
        request.setAttribute("result",clazzMList);
        return clazzMList;
    }


    @RequestMapping("/gotoedit")
    public String gotoedit(int clazzId, HttpServletRequest request, HttpServletResponse response){
        ClazzM clazz = clazzService.getOne(clazzId);
        System.out.println("clazz:" + clazz.toString());
        request.setAttribute("result",clazz);
        return "/WEB-INF/view/classM/edit.jsp";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public String edit(@RequestBody ClazzM clazzM){

        try{
            clazzService.update(clazzM);
        }catch(Exception e){
            e.printStackTrace();
            return "失败";
        }
        return "成功";
    }

    @RequestMapping("/delete")
    public String del(String id){
        clazzService.del(id);
        System.out.println(id);
        return "/demo/Clazz/gotoget";
    }

    @RequestMapping("/gotoadd")
    public String dotoadd(){
        return "/WEB-INF/view/classM/add.jsp";
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(@Valid @RequestBody ClazzM clazzM,HttpServletRequest request){
        System.out.println(clazzM);
        Integer result = 0;
        try{
            result = clazzService.add(clazzM);
        }catch (Exception e){
            request.setAttribute("result","课程信息录入错误错误");
            e.printStackTrace();
            return ResultUtils.addResult(result);
        }

        return ResultUtils.addResult(result);
    }

    @RequestMapping(value = "/getByArray")
    @ResponseBody
    public List<ClazzM> getByArray(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "size",defaultValue = "5") Integer size){
        List<ClazzM> clazzMS =  clazzService.getClassByArray(currentPage, size);
//        map.put("number",userFeedbackDTOPage.getTotalElements());
        return clazzMS;
    }

    @RequestMapping(value = "/getNumber",method = RequestMethod.POST)
    @ResponseBody
    public Integer getNumber(){
        List<ClazzM> clazzMS = clazzService.getAll();
        return clazzMS.size();
    }


}
